package com.github.postsanf.yinian.activity;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.LookHistoryAdapter;
import com.github.postsanf.yinian.adapter.MCardEditGridLayoutAdapter;
import com.github.postsanf.yinian.bean.Tag;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNCommonResponse;
import com.github.postsanf.yinian.entity.YNLoginResponse;
import com.github.postsanf.yinian.randomlayout.DividerItemDecoration;
import com.github.postsanf.yinian.randomlayout.TwoWayView;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.YNInviteSecret;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.github.postsanf.yinian.view.StyleDialog;
import com.github.postsanf.yinian.widget.TagCloudLinkView;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import com.umeng.analytics.MobclickAgent;

public class MemoryCardDetailActivity extends BaseActivity implements View.OnClickListener {
    private YNStatus mCurStatus;
    private List<String> tempUris;
    private TwoWayView McardEtwowaysView;
    private MCardEditGridLayoutAdapter mGridAdapter;
    private TextView mcarde_content_txt;
    private TextView mcarde_date_year;
    private TextView mcarde_date_mouth;
    private ScrollView mcarde_content_scroll;
    private RelativeLayout mcarde_mic;
    private TextView mcarde_mic_tx;
    private ImageView lineplay;
    private TextView mcarde_location;
    private TagCloudLinkView uploadTag;
    private AnimationDrawable anima;
    private MediaPlayer mediaPlayer;
    private RelativeLayout detail_top;
    boolean isComplete = false;
    private InputMethodManager inputMethodManager;
    private LinearLayout ll_two_pic;
    private SimpleDraweeView iv_first_pic;
    private SimpleDraweeView iv_second_pic;
    private ScrollView scrollv;
    private RecyclerView look_recycler_horizontal;
    private RelativeLayout rl_look;
    private TextView mcarde_comment;
    private ImageView mcarde_more;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_memory_card_detail);
        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        registerBroadcastReceiver();
        initDatas();
        initView();
    }

    // 广播接收
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.updataMcardDetailPic:
                    int position = intent.getIntExtra("position", 0);
                    String url = intent.getStringExtra("url");
                    for (int i = 0; i < tempUris.size(); i++) {
                        if (url.equals(tempUris.get(i))) {
                            tempUris.remove(i);
                        }
                    }
                    mGridAdapter.notifyDataSetChanged();
                    if (tempUris.size() == 1) {
                        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) detail_top.getLayoutParams();
                        lp.bottomMargin = UIHelper.dip2px(MemoryCardDetailActivity.this, 125);
                        detail_top.setLayoutParams(lp);
                        scrollv.setVisibility(View.VISIBLE);
                        ll_two_pic.setVisibility(View.GONE);
                    } else if (tempUris.size() == 2) {
                        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) detail_top.getLayoutParams();
                        lp.bottomMargin = UIHelper.dip2px(MemoryCardDetailActivity.this, 125);
                        detail_top.setLayoutParams(lp);
                        scrollv.setVisibility(View.GONE);
                        ll_two_pic.setVisibility(View.VISIBLE);
                        Uri urlFirst = Uri.parse(StringUtils.repaceImgUrl(YNUrlHelper.getStatusUrl(MemoryCardDetailActivity.this, tempUris.get(0))));
                        Uri urlSecond = Uri.parse(StringUtils.repaceImgUrl(YNUrlHelper.getStatusUrl(MemoryCardDetailActivity.this, tempUris.get(1))));
                        ImageOptHelper.showFrescoStatus(urlFirst, iv_first_pic, MemoryCardDetailActivity.this);
                        ImageOptHelper.showFrescoStatus(urlSecond, iv_second_pic, MemoryCardDetailActivity.this);
                        YNSingleton.getInstance().setCurStatus(mCurStatus);
                    } else {
                        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) detail_top.getLayoutParams();
                        lp.bottomMargin = UIHelper.dip2px(MemoryCardDetailActivity.this, 125);
                        detail_top.setLayoutParams(lp);
                        scrollv.setVisibility(View.VISIBLE);
                        ll_two_pic.setVisibility(View.GONE);
                    }
                    break;
                case ACTIONs.actionCloseAlbum:
                case ACTIONs.McardDetailFinish:
                    finish();
                    break;
            }
        }
    };

    //注册广播
    private void registerBroadcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.updataMcardDetailPic);
        myIntentFilter.addAction(ACTIONs.McardDetailFinish);
        myIntentFilter.addAction(ACTIONs.actionCloseAlbum);
        this.registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    private void hidesoftkeyword() {
        if (MemoryCardDetailActivity.this.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (MemoryCardDetailActivity.this.getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(MemoryCardDetailActivity.this
                        .getCurrentFocus().getWindowToken(), 0);
        }
    }

    private void initDatas() {
        mCurStatus = YNSingleton.getInstance().getCurStatus();
    }


    private void getUserAvatar(int userid) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(userid));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowUserHead))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNLoginResponse resUser = gson.fromJson(s, YNLoginResponse.class);
                        if (resUser != null && ReqUtils.isSuccess(resUser.getCode()) && resUser.getData().size() > 0) {
                            String avatarPic = resUser.getData().get(0).getUpic();
                            Intent intent = getIntent();
                            if (intent != null) {
                                String lookStatus = intent.getStringExtra("lookStatus");
                                if (TextUtils.equals(lookStatus, "lookStatus")) {
                                    mcarde_comment.setVisibility(View.GONE);
                                    LookHistoryAdapter lookHistoryAdapter = new LookHistoryAdapter(MemoryCardDetailActivity.this, mCurStatus, String.valueOf(application.getCurID()), avatarPic);
                                    look_recycler_horizontal.setAdapter(lookHistoryAdapter);
                                } else {
                                    mcarde_comment.setVisibility(View.VISIBLE);
                                    rl_look.setVisibility(View.GONE);
                                }
                                String memorycard = intent.getStringExtra("memorycard");
                                if (TextUtils.equals(memorycard, "memorycard")) {
                                    mcarde_comment.setVisibility(View.GONE);
                                }
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void initView() {
        rl_look = ((RelativeLayout) findViewById(R.id.rl_look));
        look_recycler_horizontal = ((RecyclerView) findViewById(R.id.look_recycler_horizontal));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MemoryCardDetailActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        look_recycler_horizontal.setLayoutManager(linearLayoutManager);
        mcarde_comment = ((TextView) findViewById(R.id.mcarde_comment));
        mcarde_more = ((ImageView) findViewById(R.id.mcarde_more));
        getUserAvatar(application.getCurID());
        ll_two_pic = ((LinearLayout) findViewById(R.id.ll_two_pic));
        iv_first_pic = ((SimpleDraweeView) findViewById(R.id.iv_first_pic));
        iv_second_pic = ((SimpleDraweeView) findViewById(R.id.iv_second_pic));
        scrollv = ((ScrollView) findViewById(R.id.scrollv));
        iv_first_pic.setOnClickListener(this);
        iv_second_pic.setOnClickListener(this);
        detail_top = (RelativeLayout) findViewById(R.id.detail_top);
        mcarde_content_txt = (TextView) findViewById(R.id.mcarde_content_txt);
        mcarde_content_txt.setTypeface(Typeface.createFromAsset(this.getAssets(), "fzqk.ttf"));
        mcarde_content_scroll = (ScrollView) findViewById(R.id.mcarde_content_scroll);
        findViewById(R.id.mcarde_back).setOnClickListener(this);
        mcarde_comment.setOnClickListener(this);
        mcarde_more.setOnClickListener(this);
        mcarde_date_year = (TextView) findViewById(R.id.mcarde_date_year);
        mcarde_date_mouth = (TextView) findViewById(R.id.mcarde_date_mouth);
        mcarde_date_year.setTypeface(Typeface.createFromAsset(this.getAssets(), "fzqk.ttf"));
        mcarde_date_mouth.setTypeface(Typeface.createFromAsset(this.getAssets(), "fzqk.ttf"));
        ((TextView) findViewById(R.id.mcarde_date_ri)).setTypeface(Typeface.createFromAsset(this.getAssets(), "fzqk.ttf"));
        mcarde_mic = (RelativeLayout) findViewById(R.id.mcarde_mic);
        mcarde_mic_tx = (TextView) findViewById(R.id.mcarde_mic_tx);
        lineplay = (ImageView) findViewById(R.id.mcard_guide_audio_play);
        mcarde_location = (TextView) findViewById(R.id.mcarde_location);
        mcarde_mic.setOnClickListener(this);
        uploadTag = (TagCloudLinkView) findViewById(R.id.mcarde_content_tag_tx);
        McardEtwowaysView = (TwoWayView) findViewById(R.id.mcarde_twoways_view);
        // McardEtwowaysView.setScrollContainer(false);
        McardEtwowaysView.setKeepScreenOn(false);
        McardEtwowaysView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.mcard_divider)));
        tempUris = mCurStatus.getUrl();
        mGridAdapter = new MCardEditGridLayoutAdapter(MemoryCardDetailActivity.this, McardEtwowaysView, tempUris, mCurStatus);
        McardEtwowaysView.setAdapter(mGridAdapter);
        if (tempUris.size() == 1) {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) detail_top.getLayoutParams();
            lp.bottomMargin = UIHelper.dip2px(MemoryCardDetailActivity.this, 125);
            detail_top.setLayoutParams(lp);
        } else if (tempUris.size() == 2) {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) detail_top.getLayoutParams();
            lp.bottomMargin = UIHelper.dip2px(MemoryCardDetailActivity.this, 125);
            detail_top.setLayoutParams(lp);
            scrollv.setVisibility(View.GONE);
            ll_two_pic.setVisibility(View.VISIBLE);
            Uri urlFirst = Uri.parse(StringUtils.repaceImgUrl(YNUrlHelper.getStatusUrl(MemoryCardDetailActivity.this, tempUris.get(0))));
            Uri urlSecond = Uri.parse(StringUtils.repaceImgUrl(YNUrlHelper.getStatusUrl(MemoryCardDetailActivity.this, tempUris.get(1))));
            ImageOptHelper.showFrescoStatus(urlFirst, iv_first_pic, MemoryCardDetailActivity.this);
            ImageOptHelper.showFrescoStatus(urlSecond, iv_second_pic, MemoryCardDetailActivity.this);
            YNSingleton.getInstance().setCurStatus(mCurStatus);
        } else {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) detail_top.getLayoutParams();
            lp.bottomMargin = UIHelper.dip2px(MemoryCardDetailActivity.this, 125);
            detail_top.setLayoutParams(lp);
        }
        if (mCurStatus.getEtext() != null && mCurStatus.getEtext().length() > 0) {
            mcarde_content_scroll.setVisibility(View.VISIBLE);
            mcarde_content_txt.setText(mCurStatus.getEtext());
        } else {
            mcarde_content_scroll.setVisibility(View.GONE);
        }
        List<Tag> tags = mCurStatus.getTags();
        if (tags != null) {
            for (int j = 0; j < tags.size(); j++) {
                if (!StringUtils.isEmpty(tags.get(j).getTagContent())) {
                    uploadTag.add(new Tag(j, tags.get(j).getTagContent()));
                }
            }
        }
        uploadTag.drawTags(R.drawable.mycard_guide_tag_white, R.color.user_color_green, 12);
        uploadTag.setOnTagSelectListener(new TagCloudLinkView.OnTagSelectListener() {
            @Override
            public void onTagSelected(Tag tag, int position, View tagView, TextView textView) {
                YNSingleton.getInstance().setCurStatus(mCurStatus);
                Intent intent = new Intent(MemoryCardDetailActivity.this, ShowTagDialog.class);
                startActivity(intent);
            }
        });
        String str = mCurStatus.getEmemorytime().split(" ")[0];
        mcarde_date_year.setText(str.split("-")[0]);
        mcarde_date_mouth.setText(str.split("-")[1] + "." + str.split("-")[2]);
        if (mCurStatus.getEplace() != null && mCurStatus.getEplace().length() > 0) {
            mcarde_location.setText(mCurStatus.getEplace());
        } else {
            mcarde_location.setText("  忆年");
        }
        mcarde_location.setOnClickListener(this);
        mediaPlayer = new MediaPlayer();
        if (StringUtils.isEmpty(mCurStatus.getEaudio())) {
            mcarde_mic_tx.setText("无声胜有声");
            lineplay.setVisibility(View.GONE);
        } else {
            anima = (AnimationDrawable) lineplay
                    .getDrawable();
            anima.setOneShot(false);
            anima.stop();
            anima.selectDrawable(2);
            try {
                mediaPlayer.setDataSource(mCurStatus.getEaudio());
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
                mcarde_mic_tx.setText("无声胜有声");
                lineplay.setVisibility(View.GONE);
                return;
            }
            lineplay.setVisibility(View.VISIBLE);
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mictime = mp.getDuration() / 1000;
                    mcarde_mic_tx.setText(mictime + "''");
                    isComplete = true;
                }
            });
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    anima.stop();
                    anima.selectDrawable(2);
                }
            });
        }
    }

    private int mictime;

    protected void showLocation() {
        RelativeLayout nameLayout = (RelativeLayout) getLayoutInflater()
                .inflate(R.layout.show_location_dialog, null);
        final Dialog nameDialog = new Dialog(MemoryCardDetailActivity.this, R.style.dialog);
        nameDialog.addContentView(nameLayout, new ViewGroup.LayoutParams(-2, -2));
        nameDialog.show();
        final TextView dialogtt = (TextView) nameLayout
                .findViewById(R.id.dialog_title);
        if (mCurStatus.getEplace() != null && mCurStatus.getEplace().length() > 0) {
            dialogtt.setText(mCurStatus.getEplace());
        } else {
            dialogtt.setText("  忆年");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mediaPlayer.pause();
        this.unregisterReceiver(mBroadcastReceiver);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mediaPlayer.pause();
        mediaPlayer.stop();
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mcarde_back:
                mediaPlayer.pause();
                mediaPlayer.stop();
                finish();
                break;
            case R.id.mcarde_location:
                Intent locaintent = new Intent(MemoryCardDetailActivity.this, ShowLocationDialog.class);
                if (mCurStatus.getEplace() != null && mCurStatus.getEplace().length() > 0) {
                    locaintent.putExtra("text", mCurStatus.getEplace());
                } else {
                    locaintent.putExtra("text", "  忆年");
                }
                startActivity(locaintent);
                break;
            case R.id.mcarde_mic:
                if (!isComplete)
                    return;
                if (mediaPlayer == null)
                    return;
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    anima.stop();
                } else {
                    mediaPlayer.start();
                    anima.start();
                }
                break;
            case R.id.mcarde_comment:
                YNSingleton.getInstance().setCurStatus(mCurStatus);
                Intent intent = new Intent(MemoryCardDetailActivity.this, MemoryCardDetailCommentActivity.class);
                intent.putExtra("mcard", "mcard");
                startActivity(intent);
                break;
            case R.id.mcarde_more:
                showPP(view);
                break;
            //记录卡片两张照片上下模式的点击查看大图
            case R.id.iv_first_pic:
//                YNSingleton.getInstance().setCurStatus(mCurStatus);
                if (!tempUris.get(0).startsWith("http"))
                    return;
                Intent intentBrowseFirst = new Intent(MemoryCardDetailActivity.this, BrowseActivity.class);
                intentBrowseFirst.putExtra("detail", true);
                intentBrowseFirst.putExtra(CommonConstants.YNPOSITION, 0);
                startActivity(intentBrowseFirst);
                break;
            case R.id.iv_second_pic:
//                YNSingleton.getInstance().setCurStatus(mCurStatus);
                if (!tempUris.get(1).startsWith("http"))
                    return;
                Intent intentBrowseSecond = new Intent(MemoryCardDetailActivity.this, BrowseActivity.class);
                intentBrowseSecond.putExtra("detail", true);
                intentBrowseSecond.putExtra(CommonConstants.YNPOSITION, 1);
                MemoryCardDetailActivity.this.startActivity(intentBrowseSecond);
                break;
            case R.id.pp_edit:
                YNSingleton.getInstance().setCurStatus(mCurStatus);
                Intent meditIntent = new Intent(MemoryCardDetailActivity.this, MemoryCardEditActivity.class);
                meditIntent.putExtra("edit", "edit");
                meditIntent.putExtra("mictime", mictime);
                startActivity(meditIntent);
                break;
            case R.id.pp_delete:
                new AlertDialog.Builder(this).setMessage("确定要删除这条动态吗？")
                        .setPositiveButton(R.string.yn_app_ok, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                doDeleteStatus();
                            }
                        })
                        .setNegativeButton(R.string.yn_app_cancel, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                            }
                        }).show();
                break;
            case R.id.pp_share:
                StyleDialog styleDialog = new StyleDialog(MemoryCardDetailActivity.this);
                styleDialog.setCanceledOnTouchOutside(true);
                styleDialog.show();
                styleDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        SharedPreferences sp = getSharedPreferences("isApart", Context.MODE_PRIVATE);
                        int isApart = sp.getInt("isApart", 0);
                        if (isApart == 1) {
                            Intent intent = new Intent(MemoryCardDetailActivity.this, SharePhotoActivity.class);
                            intent.putStringArrayListExtra(ACTIONs.aAddData, (ArrayList<String>) mCurStatus.getUrl());
                            intent.putExtra("type", "card_item_share");
                            startActivity(intent);
                            SharedPreferences.Editor edit = sp.edit();
                            edit.putInt("isApart", 0);
                            edit.commit();
                        } else if (isApart == 2) {
                            String url = getResources().getString(R.string.SHARE_JIYI);
                            String sec = null;
                            try {
                                sec = YNInviteSecret.encryptDES("eventid=" + mCurStatus.getEid(), getResources().getString(R.string.UNKNOW_ID));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            url = url + sec;
                            Intent intent = new Intent(MemoryCardDetailActivity.this, StatusShareActivity.class);
                            intent.putExtra("share", "share");
                            intent.putExtra("type", "card_item_share");
                            intent.putExtra("avatarPaths", YNUrlHelper.getAvatarUrl(MemoryCardDetailActivity.this, mCurStatus.getPublishUser().getUpic()));
                            intent.putExtra("fristpic", mCurStatus.getThumbnail().get(0));
                            intent.putExtra("title", mCurStatus.getPublishUser().getUnickname() + "。" + "有一段美好的时光@你！");
                            intent.putExtra("desc", "秘密花园限时开放哦！");
                            intent.putExtra("url", url);
                            startActivity(intent);
                            SharedPreferences.Editor edit = sp.edit();
                            edit.putInt("isApart", 0);
                            edit.commit();
                        } else if (isApart == 3) {
                            String url = getResources().getString(R.string.SHARE_JIYI);
                            String sec = null;
                            try {
                                sec = YNInviteSecret.encryptDES("eventid=" + mCurStatus.getEid(), getResources().getString(R.string.UNKNOW_ID));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            url = url + sec;
                            Intent intent = new Intent(MemoryCardDetailActivity.this, PicWallShareActivity.class);
                            intent.putExtra("QRcode", "QRcode");
                            intent.putExtra("UrlShare", url);
                            intent.putExtra("firstPic", mCurStatus.getThumbnail().get(0));
                            intent.putExtra("nickName", mCurStatus.getPublishUser().getUnickname() + "和你分享了一张ta制作的记录卡片");
                            intent.putExtra("QRtext", "长按识别二维码查看全部照片");
                            intent.putExtra("above", "记录卡片：记录某个重要时刻完整细节的图文卡片");
                            startActivity(intent);
                            SharedPreferences.Editor edit = sp.edit();
                            edit.putInt("isApart", 0);
                            edit.commit();
                        }
                    }
                });
                break;
        }
    }

    private PopupWindow window;

    protected void showPP(View parent) {
        View v = LayoutInflater.from(MemoryCardDetailActivity.this).inflate(R.layout.mcard_detail_pp,
                null);
        View pp_line1 = v.findViewById(R.id.pp_line1);
        View pp_line2 = v.findViewById(R.id.pp_line2);
        TextView edit = (TextView) v.findViewById(R.id.pp_edit);
        edit.setOnClickListener(this);
        TextView delete = (TextView) v.findViewById(R.id.pp_delete);
        delete.setOnClickListener(this);
        v.findViewById(R.id.pp_share).setOnClickListener(this);
        boolean userStatus = getIntent().getBooleanExtra("userStatus", false);
        if (mCurStatus.getPublishUser() != null && mCurStatus.getPublishUser().getUserid() == application.getCurID() || userStatus) {
            window = new PopupWindow(v, UIHelper.dip2px(MemoryCardDetailActivity.this, 80), UIHelper.dip2px(MemoryCardDetailActivity.this, 120));
        } else {
            window = new PopupWindow(v, UIHelper.dip2px(MemoryCardDetailActivity.this, 80), UIHelper.dip2px(MemoryCardDetailActivity.this, 40));
            edit.setVisibility(View.GONE);
            delete.setVisibility(View.GONE);
            pp_line1.setVisibility(View.GONE);
            pp_line2.setVisibility(View.GONE);
        }
        window.setAnimationStyle(R.style.pp_AnimationPreview);
        window.setTouchable(true);
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new BitmapDrawable());
        window.showAsDropDown(parent);
    }


    // 删除该条动态
    private void doDeleteStatus() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_EVENT_ID, String.valueOf(mCurStatus.getEid()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynDeleteEvent))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNCommonResponse resDelete = gson.fromJson(s, YNCommonResponse.class);
                        if (ReqUtils.isSuccess(resDelete.getCode())) {
                            Intent mIntent = new Intent(ACTIONs.actionStatus);
                            // 同步删除动态
                            mIntent.putExtra(ACTIONs.IsAdd, false);
                            mIntent.putExtra(ACTIONs.StatusId, mCurStatus.getEid());
                            //发送广播,通知更新
                            sendBroadcast(mIntent);
                            sendBroadcast(new Intent(ACTIONs.actionGridWallUpdate));
                            finish();
                            if (getIntent().getBooleanExtra("detail", false)) {
                                sendBroadcast(new Intent(ACTIONs.McardDetailFinish));
                            }
                        } else {
                            showToast("删除失败");
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    protected void onResume() {
        super.onResume();
        hidesoftkeyword();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
